package cn.fl.customer.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cmm.comm.facade.framework.api.ISysDictionaryApiService;
import cmm.mid.core.framework.page.KeyValueVO;
import cmm.mid.core.framework.page.Pager;
import cn.cmm.base.framework.exception.BaseException;
import cn.cmm.base.framework.utils.BaseLogUtils;
import cn.cmm.base.framework.utils.CollectionUtils;
import cn.cmm.base.framework.utils.StringUtils;
import cn.fl.customer.service.ICstCorpTeamService;
import cn.fl.customer.transform.CstCorpTeamVOTransform;
import cn.fl.customer.vo.CstCorpTeamVO;
import cn.fl.framework.base.context.CurrentThreadContext;
import cn.fl.framework.constant.DictionaryConstant;
import fl.customer.facade.customer.api.ICustomerApiService;
import fl.customer.facade.customer.condition.CstCorpTeamQueryCondition;
import fl.customer.facade.customer.dto.CstCorpTeamDTO;

/**
 * 企业团队服务接口实现
 */
@Service
public class CstCorpTeamServiceImpl implements ICstCorpTeamService {

	/**
	 * 日志
	 */
	private static final Logger logger = LoggerFactory.getLogger(CstCorpTeamServiceImpl.class);
	
	/**
	 * 客户 API服务
	 */
	@Autowired
	private ICustomerApiService customerApiService;
	
	/**
	 * 本地数据字典
	 */
	@Autowired
	private ISysDictionaryApiService sysDictionaryApiService;

	@Override
	public CstCorpTeamVO selectById(Long id) throws BaseException{
		if(StringUtils.isEmpty(id))return null;
		String sysCd = CurrentThreadContext.getFlServiceSysCd();
		String orgCd = CurrentThreadContext.getCurrentOrgCd();
		CstCorpTeamDTO dto = this.customerApiService.selectCstCorpTeamById(id, sysCd, orgCd);
		this.setDicInfo(dto);
		return CstCorpTeamVOTransform.toVO(dto);
	}

	@Override
	public Long add(CstCorpTeamVO entity) throws BaseException{
		if(null == entity)return null;
		return this.customerApiService.addCstCorpTeam(CstCorpTeamVOTransform.toDTO(entity));
	}

	@Override
	public boolean removeByIds(List<Long> idList) throws BaseException{
		if(CollectionUtils.isEmpty(idList))return false;
		return this.customerApiService.removeCstCorpTeamByIds(idList);
	}

	@Override
	public boolean modify(CstCorpTeamVO entity) throws BaseException{
		if(null == entity)return false;
		return this.customerApiService.modifyCstCorpTeam(CstCorpTeamVOTransform.toDTO(entity));
	}

	@Override
	public Pager searchListPage(Pager page) throws BaseException{
		if(null == page)page = new Pager();
		return this.customerApiService.searchCstCorpTeamPage(page);
	}

	@Override
	public List<KeyValueVO> searchMTypCdList() throws BaseException{
		return this.sysDictionaryApiService.getKvList(DictionaryConstant.CODE_GRP_M_TYP_CD, CurrentThreadContext.getCurrentSysCd(), CurrentThreadContext.getCurrentOrgCd());
	}
	
	
	@Override
	public List<CstCorpTeamVO> searchList(CstCorpTeamQueryCondition condition) throws BaseException {
		List<CstCorpTeamDTO> list = this.customerApiService.searchCstCorpTeamList(condition);
		return CstCorpTeamVOTransform.toVOList(list);
	}

	/**
	 * 设置数据字典项目
	 * @param dto
	 */
	private void setDicInfo(CstCorpTeamDTO dto){
		if(null == dto)return;
		Map<String, Map<String, String>>  map = null;
		try {
			String sysCd = CurrentThreadContext.getCurrentSysCd();
			String orgCd = CurrentThreadContext.getCurrentOrgCd();
			List<String> codeGrpCdList = new ArrayList<>();
			codeGrpCdList.add(DictionaryConstant.CODE_GRP_M_TYP_CD);
			codeGrpCdList.add(DictionaryConstant.CODE_GRP_CERT_TYP_CD);
			map = this.sysDictionaryApiService.searchMultiGroupToMap(sysCd, orgCd, codeGrpCdList);
			
			if(CollectionUtils.isMapEmpty(map)){
				map = new HashMap<>();
			}
		} catch (BaseException e) {
			BaseLogUtils.info(logger, "setDicInfo", "获取数据字典失败!" + e.getMessage());
		} catch (Exception e) {
			BaseLogUtils.error(logger, "setDicInfo", "获取数据字典失败!" + e.getMessage());
		}
		
		Map<String, String> mTypCdMap = map.get(DictionaryConstant.CODE_GRP_M_TYP_CD);
		if(CollectionUtils.isMapEmpty(mTypCdMap)){
			mTypCdMap = new HashMap<>();
		}
		dto.setmTypCdNm(mTypCdMap.get(dto.getMTypCd()));
		
		Map<String, String> certTypCdMap = map.get(DictionaryConstant.CODE_GRP_CERT_TYP_CD);
		if(CollectionUtils.isMapEmpty(certTypCdMap)){
			certTypCdMap = new HashMap<>();
		}
		dto.setCertTypCdNm(certTypCdMap.get(dto.getCertTypCd()));
	}

}
